package cn.lysj.framework.foodv2.controller;

import cn.lysj.framework.base.BaseController;
import cn.lysj.framework.common.utils.web.ParamUtil;
import cn.lysj.framework.common.utils.web.Resp;
import cn.lysj.framework.foodv2.domain.Food;
import cn.lysj.framework.foodv2.domain.StandardLevOne;
import cn.lysj.framework.foodv2.domain.StandardLevTwo;
import cn.lysj.framework.foodv2.service.StandardLevOneService;
import cn.lysj.framework.foodv2.service.StandardLevTwoService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @author drj
 * @description 二级规格控制层
 * @create 2018-11-09 11:30:11
 **/
@Slf4j
@RestController
@Api(description = "二级规格管理接口")
@RequestMapping("/standard_lev_two")
public class StandardLevTwoController extends BaseController<StandardLevTwo> {

    @Resource
    private StandardLevOneService standardLevOneService;
    @Resource
    private StandardLevTwoService standardLevTwoService;

    @Override
    public StandardLevTwoService getService() {
        return standardLevTwoService;
    }


    /**
     * @author drj
     * @date 2018/11/11 16:30
     * @Description:修改菜品信息
     */
    @PostMapping("/update_money")
    public Resp updateMoney(String standardTwoInfo) {

        String[] itemArray = standardTwoInfo.split(";");
        for (String s : itemArray) {
            String[] itemArray1 = s.split("-");
            StandardLevTwo standardLevTwo = standardLevTwoService.getRepository().findOne(itemArray1[0]);
            try {
                standardLevTwo.setMoney(new BigDecimal(itemArray1[1]));
            } catch (Exception e) {
                return new Resp(Resp.PARAM_ERROR, "设置失败,价格必须为数字!");
            }
            standardLevTwoService.getRepository().save(standardLevTwo);
        }
        return new Resp("修改成功");
    }

    @Override
    public Resp save(StandardLevTwo model) {
        if (ParamUtil.isBlank(model.getStandardLevOneId())) {
            return new Resp(Resp.PARAM_ERROR, "参数错误");
        }
        StandardLevOne standardLevOne = standardLevOneService.findOne(model.getStandardLevOneId());
        if (null == standardLevOne) {
            return new Resp(Resp.PARAM_ERROR, "参数错误");
        }
        if (ParamUtil.isBlank(model.getName())) {
            return new Resp(Resp.PARAM_ERROR, "请填写二级规格名称");
        }
        StandardLevTwo standardLevTwo = new StandardLevTwo();
        standardLevTwo.setStandardLevOneId(model.getStandardLevOneId());
        standardLevTwo.setName(model.getName());
        standardLevTwo.setMoney(BigDecimal.ZERO);
        standardLevTwo = standardLevTwoService.save(standardLevTwo);
        return new Resp(standardLevTwo);
    }
}
